package qc.module.platform.mapper;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.DataManagementTypeEnum;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.platform.dto.dbTableSql.DBTableSqlDto;
import qc.module.platform.dto.dbTableSql.DBTableSqlSimpleDto;
import qc.module.platform.entity.QcDbTableSql;

import java.util.List;

/**
 * DBTableSqlMapper
 *
 * @author QuCheng Tech
 * @since 2024/1/29
 */
@Mapper
public interface DBTableSqlMapper {
    DBTableSqlMapper MAPPER = Mappers.getMapper(DBTableSqlMapper.class);

    @Mapping(target = "optype", ignore = true)
    @Mapping(target = "flag", ignore = true)
    QcDbTableSql toEntity(DBTableSqlDto dto);


    /**
     * 新增DTO转换为Entity
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    default QcDbTableSql dtoToEntity(DBTableSqlDto dto) {
        //DTO转换为Entity
        QcDbTableSql en = DBTableSqlMapper.MAPPER.toEntity(dto);

        //处理sql操作类型，非空时进行转换，空值时使用默认
        Integer optype = null;
        if (!StringUtils.isBlank(dto.getOptype())) {
            try {
                optype = Integer.parseInt(dto.getOptype());
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setOptype(getSqlDataOperateTypeEnum(optype));

        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (!StringUtils.isBlank(dto.getFlag())) {
            try {
                flag = Integer.parseInt(dto.getFlag());
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    /**
     * 修改DTO转换为Entity
     *
     * @param dto 修改DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    default QcDbTableSql editDtoToEntity(DBTableSqlDto dto) {
        //DTO转换为Entity
        QcDbTableSql en = DBTableSqlMapper.MAPPER.dtoToEntity(dto);

        // 表主键单独在外处理
        en.setTbcode(null);
        en.setCode(null);
        
        return en;
    }

    @Mapping(target = "optype", source = "optype.index")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "editflag", source = "editflag")
    DBTableSqlDto toDto(QcDbTableSql en);

    @Mapping(target = "optype", source = "optype.index")
    DBTableSqlSimpleDto toSimpleDto(QcDbTableSql en);

    List<DBTableSqlDto> toDtoList(List<QcDbTableSql> list);

    List<DBTableSqlSimpleDto> toSimpleDtoList(List<QcDbTableSql> list);

    static DataManagementTypeEnum getSqlDataOperateTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (DataManagementTypeEnum item : DataManagementTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return DataManagementTypeEnum.SELECT;
    }

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ResourceStatusFlagEnum.NORMAL;
    }
}
